//
//  TestCode.m
//  Pods
//
//  Created by sungeo on 15/9/5.
//
//

#import "TestCode.h"
#import "WMModels.h"
#import <NSDate+DateTools.h>
#import <RandomSequence.h>
#import <NSObject+GLPubSub.h>

NSString * const AddWeightNote = @"addWeightNote";

@implementation TestCode

+ (instancetype)sharedInstance{
    static TestCode * sInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sInstance = [[TestCode alloc] init];
    });
    
    return sInstance;
}

/**
 *  下面都是测试代码
 */
- (void)showTestMenu{
    UIActionSheet * ac = [[UIActionSheet alloc] initWithTitle:@"测试" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:nil, nil];
    [ac addButtonWithTitle:@"修改身高测试"];
    [ac addButtonWithTitle:@"添加从昨天开始往后数十条体重"];
    [ac addButtonWithTitle:@"BMI计算测试"];
    [ac addButtonWithTitle:@"查询最近记录"];
    [ac addButtonWithTitle:@"清空所有体重记录"];
    [ac addButtonWithTitle:@"添加体重"];
    [ac showInView:[[UIApplication sharedApplication] keyWindow]];
}

- (void)testEditHeight{
    WMAppSetting * st = [WMAppSetting sharedInstance];
    NSInteger height = [[RandomSequence defaultSequence] nextIntegerFrom:175 to:190];
    NSLog(@"测试身高：%@", @(height));
    st.height = @(height);
    [st syncData];
}

- (void)testAddWeights{
    NSLog(@"添加从昨天开始往后数十条体重");
    
    for (NSInteger i = 1; i <= 3; i ++) {
        WMWeight * weight = [[WMWeight alloc] initWithValue:@(100 - i)];
        [weight setLocalProfileImage:[UIImage imageNamed:@"test.png"]];
        NSDate * date = [NSDate date];
        // 添加一条之前的记录
        date = [date dateBySubtractingDays:i];
        weight.measuringDate = date;
        if(! [[WMDataCache sharedInstance] addWeight:weight fromCloud:NO]){
            NSLog(@"添加失败");
        }
    }
}

- (void)testBMICalculation{
    float height = [[RandomSequence defaultSequence] nextIntegerFrom:170 to:200];
    NSInteger weight = [[RandomSequence defaultSequence] nextIntegerFrom:60 to:90];
    NSNumber * bmi = [WMHelper BMIForWeight:@(weight) height:@(height)];
    UIAlertView * av = [[UIAlertView alloc] initWithTitle:[bmi stringValue] message:[NSString stringWithFormat:@"weight:%@ height:%@", @(weight), @(height)] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [av show];
}

- (void)testLatestWeight{
    WMWeight * latest = [[WMDataCache sharedInstance] latestWeight];
    NSString * details = [latest.measuringDate descriptionWithLocale:[NSLocale currentLocale]];
    UIAlertView * av = [[UIAlertView alloc] initWithTitle:@"最新记录" message:details delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [av show];
}

#pragma mark UI Action sheet delegate
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        [self testEditHeight];
    }else if(buttonIndex == 2){
        [self testAddWeights];
    }else if(buttonIndex == 3){
        [self testBMICalculation];
    }else if (buttonIndex == 4){
        [self testLatestWeight];
    }else if (buttonIndex == 5){
        [[WMDataCache sharedInstance] deleteAllRecords];
    }else if (buttonIndex == 6){
        [self publish:AddWeightNote];
    }
}


@end
